home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1995…tember: Reference Library / Dev.CD Sep 95 RL / Dev.CD Sep 95 RL.toast / mac / Technical Documentation / develop / develop Issue 20 code / Pict Tricks / CLUTLess / SimpleInC.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-07-28  |  6.7 KB  |  306 lines  |  [TEXT/MMCC]

  1.  
  2. /*     
  3.                             CLUTLess
  4.                         
  5.                             
  6.             In some instances it is desireable to store picts stripped of
  7.             CLUTs in order to save some room in the disk. This sample shows
  8.             how to create such picts and how to properly display them back.
  9.             
  10.             SimpleInC.c -- initialization stuff and event loop
  11.             CLUTLess.c  -- code that does the creation of the clut less pict;
  12.                            shows also how to display the pictures.
  13.     
  14. */
  15.  
  16. #include "SimpleInC.h"
  17.  
  18. short main()
  19. {
  20.     Rect                    screenRect;
  21.     Rect                    dragRect;
  22.     Rect                    pictRect;
  23.     
  24.     EventRecord             myEvent;
  25.     WindowPtr                theActiveWindow;
  26.     WindowPtr                whichWindow;
  27.     Boolean                 Result;
  28.     
  29.     /*
  30.      * Initialization traps
  31.      */
  32.     InitGraf(&qd.thePort); 
  33.     InitFonts();
  34.     FlushEvents(everyEvent, 0);
  35.     InitWindows();
  36.     InitMenus();
  37.     TEInit();
  38.     InitDialogs(nil); 
  39.     InitCursor();
  40.     
  41.     MaxApplZone();
  42.     
  43.     /* initializes QD procs for our purposes */
  44.     InitProcs(&myProcs);
  45.     
  46.     /* ************************************************************** */
  47.     
  48.     setupMenus();            
  49.     
  50.     screenRect = qd.screenBits.bounds;
  51.     SETRECT(&dragRect, 4, 20 + 4, screenRect.right-4, screenRect.bottom-4);
  52.     
  53. /*     here we create a window to display our picture and we allocate a handle
  54.     for the palette we expect to associate with the window.
  55. */
  56.     myWindow = GetNewCWindow(windowID, nil, (WindowPtr) -1);
  57.     SetPort(myWindow);
  58.     
  59.     // THIS IS THE PLACE WHERE WE GO AND PREPARE THE IMAGE TO BE SAVED AND DISPLAYED
  60.     //  Make call to get and set all
  61.     if ( ! ( SetupPictures() )) return 0;
  62.     
  63.     pictRect = (*gOrigPict)->picFrame;
  64.     
  65.     SizeWindow(myWindow, pictRect.right-pictRect.left, pictRect.bottom-pictRect.top, false);
  66.     ShowWindow(myWindow);
  67.     
  68.     if (! gSharedClut )         // the picture did not have a 'clut' with it
  69.       DisableItem(MyMenus[stuffMenu], mClutPict);
  70.     CheckItem(MyMenus[stuffMenu], mUnmodPict, true);
  71.  
  72.     DoneFlag = false;
  73.     
  74.     for ( ;; ) {
  75.         if (DoneFlag) {
  76.             break;        /* from main event loop */
  77.         }
  78.         /*
  79.          * Main Event tasks:
  80.          */
  81.         SystemTask();
  82.         theActiveWindow = FrontWindow();        /* Used often, avoid repeated calls */
  83.  
  84.         Result = GetNextEvent(everyEvent, &myEvent);
  85.  
  86.         switch (myEvent.what) {
  87.             case mouseDown:
  88.                     switch (FindWindow(myEvent.where, &whichWindow)) {
  89.                     case inSysWindow:
  90.                         SystemClick(&myEvent, whichWindow);
  91.                         break;
  92.  
  93.                     case inMenuBar:
  94.                         {
  95.                         doCommand(MenuSelect(myEvent.where));
  96.                         }
  97.                         break;
  98.  
  99.                     case inDrag:
  100.                         DragWindow(whichWindow, myEvent.where, &dragRect);
  101.                         break;
  102.  
  103.                     case inGrow:
  104.                         /* There is no grow box. (Fall through) */
  105.  
  106.                     case inContent:
  107.                       if (whichWindow != theActiveWindow) {
  108.                             SelectWindow(whichWindow);
  109.                         }
  110.                     default:
  111.                         break;
  112.                     }/*endsw FindWindow*/
  113.                     break;
  114.  
  115.             case keyDown:
  116.             case autoKey:
  117.                 if (myWindow == theActiveWindow) {
  118.                     if (myEvent.modifiers & cmdKey) {
  119.                         doCommand(MenuKey(myEvent.message & charCodeMask));
  120.                     }
  121.                     }
  122.                 break;
  123.                 
  124.             case activateEvt:
  125.                 if ((WindowPtr) myEvent.message == myWindow) {
  126.                     if (myEvent.modifiers & activeFlag) {
  127.                     /*    TEActivate(TextH); */
  128.                         DisableItem(MyMenus[editMenu], undoCommand);
  129.                     }
  130.                     }
  131.                 break;
  132.  
  133.             case updateEvt:
  134.  
  135.                 if ((WindowPtr) myEvent.message == myWindow) {
  136.                     BeginUpdate(myWindow);
  137.                         SetPort(myWindow);
  138.                         DrawPicture(gCurrPict, &((*gCurrPict) -> picFrame)); /* draw the thing */
  139.                     EndUpdate(myWindow);
  140.                 }
  141.             default:
  142.                 break;
  143.  
  144.         }/*endsw myEvent.what*/
  145.  
  146.     }/*endfor Main Event loop*/
  147.     return 0;        /* Return from main() to allow C runtime cleanup */
  148. }
  149.  
  150. void setupMenus(void)
  151. {
  152.     extern        MenuHandle    MyMenus[];
  153.     register    MenuHandle    *pMenu;
  154.  
  155.     MyMenus[appleMenu] = GetMenu(appleID);
  156.     AddResMenu(MyMenus[appleMenu], (ResType) 'DRVR');
  157.     /*
  158.      * Now the  menus.
  159.      */
  160.      
  161.     MyMenus[fileMenu] = GetMenu(fileID);
  162.     MyMenus[editMenu] = GetMenu(editID);
  163.     MyMenus[stuffMenu] = GetMenu(stuffID);
  164.  
  165.     for (pMenu = &MyMenus[0]; pMenu < &MyMenus[menuCount]; ++pMenu) {
  166.         InsertMenu(*pMenu, 0);
  167.     }
  168.  
  169.     DrawMenuBar();
  170.  
  171.     return;
  172. }
  173.  
  174. void showAboutMeDialog(void)
  175. {
  176.     GrafPtr     savePort;
  177.     DialogPtr    theDialog;
  178.     short        itemType;
  179.     Handle        itemHdl;
  180.     Rect        itemRect;
  181.     short        itemHit;
  182.  
  183.     GetPort(&savePort);
  184.     theDialog = GetNewDialog(aboutMeDLOG, nil, (WindowPtr) -1);
  185.     SetPort(theDialog);
  186.  
  187.     GetDItem(theDialog, authorItem, &itemType, &itemHdl, &itemRect);
  188.     SetIText(itemHdl, "\pGuillermo A. Ortiz, MacDTS");
  189.     GetDItem(theDialog, languageItem, &itemType, &itemHdl, &itemRect);
  190.     SetIText(itemHdl, "\pC");
  191.  
  192.     do {
  193.         ModalDialog(nil, &itemHit);
  194.     } while (itemHit != okButton);
  195.  
  196.     CloseDialog(theDialog);
  197.  
  198.     SetPort(savePort);
  199.     return;
  200. }
  201. /*
  202.  * Process mouse clicks in menu bar
  203.  */
  204.  
  205. Boolean hilited = false;
  206.  
  207. void doCommand(long mResult)
  208. {
  209.     long                 theMenu, theItem;
  210.     unsigned char        daName[256];
  211.     GrafPtr             savePort;
  212.     extern MenuHandle    MyMenus[];
  213.     extern Boolean        DoneFlag;
  214.     extern void         showAboutMeDialog();
  215.  
  216.     theItem = LOWORD(mResult);
  217.     theMenu = HIWORD(mResult);        /* This is the resource ID */
  218.  
  219.     switch (theMenu) {
  220.         case appleID:
  221.             if (theItem == aboutMeCommand) {
  222.                 showAboutMeDialog();
  223.             } else {
  224.                 GetItem(MyMenus[appleMenu], theItem, daName);
  225.                 GetPort(&savePort);
  226.                 (void) OpenDeskAcc(daName);
  227.                 SetPort(savePort);
  228.             }
  229.             break;
  230.  
  231.         case fileID:
  232.             switch (theItem) {
  233.                 case printCommand:
  234.                     break;
  235.                 case pageCommand:
  236.                     break;
  237.                 case quitCommand:
  238.                     DoneFlag = true;            /* Request exit */
  239.                     break;
  240.                 default:
  241.                     break;
  242.             }
  243.             break;
  244.  
  245.         case editID:
  246.             /*
  247.              * If this is for a 'standard' edit item,
  248.              * run it through SystemEdit first.
  249.              * SystemEdit will return FALSE if it's not a system window.
  250.              */
  251.             if ((theItem <= clearCommand) && SystemEdit(theItem-1)) {
  252.                 break;
  253.             }
  254.             switch (theItem) {
  255.                 case undoCommand:
  256.                 case cutCommand:
  257.                 case copyCommand:
  258.                 case pasteCommand:
  259.                 case clearCommand:
  260.                 default:
  261.                     break;
  262.             } /*endsw theItem*/
  263.             break;
  264.  
  265.         case stuffID:
  266.             /* uncheck all the items to begin with */
  267.             CheckItem(MyMenus[stuffMenu], mUnmodPict, false);
  268.             CheckItem(MyMenus[stuffMenu], mNoClutPict, false);
  269.             CheckItem(MyMenus[stuffMenu], mClutPict, false);
  270.             
  271.             switch (theItem) {
  272.                 case mUnmodPict:
  273.                      gCurrPict = gOrigPict;    /* draw of original pict */
  274.                      InvalRect(&(myWindow->portRect));    
  275.                      myWindow -> grafProcs = nil;
  276.                      CheckItem(MyMenus[stuffMenu], mUnmodPict, true);
  277.                      break;
  278.                 case mNoClutPict:
  279.                      gCurrPict = gModPict;    /* draw of clut less pict */
  280.                      InvalRect(&(myWindow->portRect));    
  281.                      myWindow -> grafProcs = nil;
  282.                      CheckItem(MyMenus[stuffMenu], mNoClutPict, true);
  283.                      break;
  284.                 case mClutPict:
  285.                      gCurrPict = gModPict;    /* draw of clut less pict */
  286.                      InvalRect(&(myWindow->portRect));
  287.                      /* but use the proc to add color table to it */
  288.                      myWindow -> grafProcs = (QDProcsPtr)(& myProcs); 
  289.                      CheckItem(MyMenus[stuffMenu], mClutPict, true);
  290.                      break;
  291.                 default:
  292.                     break;
  293.             }
  294.             break;
  295.  
  296.         default:
  297.             break;
  298.  
  299.     }/*endsw theMenu*/
  300.  
  301.     HiliteMenu(0);
  302.  
  303.     return;
  304. }
  305.  
  306.